﻿using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {
	

	public float velocity = 1f;

	public Transform rayTop;
	public Transform rayBottom;

	public LayerMask detectWhat;

	public bool colliding;

	Rigidbody2D rigid;
	Animator anim;

	void Start () {
		anim = GetComponent<Animator> ();
		rigid = GetComponent<Rigidbody2D> ();	
	
	}

	void Update () {

		if(!anim.GetBool ("isKill"))
		transform.position += new Vector3 (velocity * Time.deltaTime, 0, 0);
		
		colliding = Physics2D.Linecast (rayTop.position, rayBottom.position, detectWhat);
		
		if (colliding) {		
			transform.localScale = new Vector2 (transform.localScale.x * -1, transform.localScale.y);
			velocity *= -1;
		}	


	}

	void OnDrawGizmos() {

		Gizmos.color = Color.blue;
		Gizmos.DrawLine (rayTop.position, rayBottom.position);

	}
}
